package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Long save(User user) {
//        1密码加密2设置时间
        String mdPassword = SecureUtil.md5(user.getPassword());
        user.setPassword(mdPassword);
        user.setCreated(new Date());
        user.setUpdated(new Date());
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public User findByPhone(String phone) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",phone);
        List<User> list = userMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }
}
